﻿package com.tudou.v2
{
    import com.tudou.events.*;
    import com.tudou.track.*;
    import flash.events.*;
    import flash.utils.*;

    public class V2AdLoader extends V2Loader
    {
        protected var _timeout:int = 0;
        protected var timeoutTimer:Timer;

        public function V2AdLoader()
        {
            return;
        }// end function

        public function set timeout(param1:int) : void
        {
            _timeout = param1;
            return;
        }// end function

        override protected function v2SecurityErrHlr(event:SecurityErrorEvent) : void
        {
            clearTimeout();
            super.v2SecurityErrHlr(event);
            return;
        }// end function

        override protected function v2IOErrHlr(event:IOErrorEvent) : void
        {
            clearTimeout();
            super.v2IOErrHlr(event);
            return;
        }// end function

        override protected function v2LoadedHlr(event:Event) : void
        {
            clearTimeout();
            super.v2LoadedHlr(event);
            return;
        }// end function

        override protected function startLoad() : void
        {
            if (_timeout > 0)
            {
                initTimeout();
                startTimeout();
            }
            super.startLoad();
            return;
        }// end function

        protected function initTimeout() : void
        {
            if (timeoutTimer)
            {
                clearTimeout();
            }
            timeoutTimer = new Timer(_timeout, 1);
            timeoutTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeoutHlr);
            return;
        }// end function

        protected function clearTimeout() : void
        {
            if (timeoutTimer)
            {
                timeoutTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timeoutHlr);
                timeoutTimer.stop();
            }
            timeoutTimer = null;
            return;
        }// end function

        protected function timeoutHlr(event:TimerEvent) : void
        {
            close();
            clearTimeout();
            ErrorTracker.send(baseInfo.sourceId, ErrorTracker.AD_V2_TIMEOUT, 0, url);
            dispatchEvent(new V2LoaderEvent(V2LoaderEvent.TIMEOUT_ERROR));
            return;
        }// end function

        protected function startTimeout() : void
        {
            if (timeoutTimer)
            {
                timeoutTimer.start();
            }
            return;
        }// end function

    }
}
